package com.xinlong.controller;

import com.xinlong.entity.Guide;
import com.xinlong.entity.Vehicle;
import com.xinlong.entity.product;
import com.xinlong.service.IGuideService;
import com.xinlong.service.impl.VehiceServiceimpl;
import com.xinlong.service.productservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/Product")
public class Productcontroller {

    @Autowired
    private productservice productservic;

    @Autowired
    private VehiceServiceimpl vehiceServiceimpl;

    @Autowired
    private IGuideService guideService;



    @RequestMapping("/opensave.do")
    public ModelAndView opensave() throws Exception {
        ModelAndView mv=new ModelAndView();
        List<Vehicle> vehicles=vehiceServiceimpl.selectVehicle();
        mv.addObject("vechiclelist",vehicles);
        mv.setViewName("product-add");
        return mv;
    }


//    @RequestMapping("/save.do")
//    public String save(product produc) throws Exception {
//        System.out.println(produc+"----------------------");
//        productservic.save(produc);
//        return "redirect:select.do";
//    }


    @RequestMapping("/delteProduct.do")
    public String delproduct(Integer id) {
        productservic.delproduct(id);
        return "redirect:select.do";
    }

    //修改
    @RequestMapping("/update.do")
    public String update(String id,String productNum,String productName,String cityName,String departureTimeStr,String endTimeStr,String productPrice,String productDesc,String productStatusStr,String vehicleid) throws Exception {
//        System.out.println(productNum+"----------------------");
        product produc=new product();
        produc.setId(id);
        produc.setProductNum(productNum);
        produc.setProductName(productName);
        produc.setCityName(cityName);
        produc.setDepartureTimeStr(departureTimeStr);
        produc.setEndTimeStr(endTimeStr);
        produc.setProductPrice(Double.parseDouble(productPrice));
        produc.setProductDesc(productDesc);
        produc.setProductStatus(Integer.parseInt(productStatusStr));
        produc.setVehicleid(Integer.parseInt(vehicleid));
//        System.out.println(produc);

        productservic.upproduct(produc);
        return "redirect:select.do";
    }

    //打开修改
    @RequestMapping("/openupdate.do")
    public ModelAndView openupdate(String id) throws Exception {
        ModelAndView mv=new ModelAndView();
        product product=productservic.findById(id);
        mv.addObject("product",product);
        List<Vehicle> vehicles=vehiceServiceimpl.selectVehicle();
        mv.addObject("vechiclelist",vehicles);
        mv.setViewName("product-update");
        return mv;
    }




    @RequestMapping("/save.do")
    public String save(String productNum,String productName,String cityName,String departureTimeStr,String endTimeStr,String productPrice,String productDesc,String productStatusStr,String vehicleid) throws Exception {
//        System.out.println(productNum+"----------------------");
        product produc=new product();
        produc.setProductNum(productNum);
        produc.setProductName(productName);
        produc.setCityName(cityName);
        produc.setDepartureTimeStr(departureTimeStr);
        produc.setEndTimeStr(endTimeStr);
        produc.setProductPrice(Double.parseDouble(productPrice));
        produc.setProductDesc(productDesc);
        produc.setProductStatus(Integer.parseInt(productStatusStr));
        produc.setVehicleid(Integer.parseInt(vehicleid));
//        System.out.println(produc);

        productservic.save(produc);
        return "redirect:select.do";
    }

    @RequestMapping("/select.do") //.do个人习惯在web.xml中拦截.do
    public ModelAndView select() throws Exception {
        ModelAndView mv=new ModelAndView();
        List<product> products=productservic.productSelect();
        mv.addObject("productList",products);
        mv.setViewName("product-list");
        return mv;
    }

    @RequestMapping("/selectbyOut.do")
    public ModelAndView selectbyproductStatus() throws Exception {
        ModelAndView mv=new ModelAndView();
        List<product> products=productservic.findByproductStatus(3);
        mv.addObject("productList",products);
        mv.setViewName("product-list");
        return mv;
    }




    @RequestMapping("/ProductandGuideAll.do")
    public ModelAndView ProductandGuideAll(String id){
        ModelAndView mv=new ModelAndView();
        product products=productservic.findById(id);
        List<Guide> guides=guideService.AllGuide(Integer.parseInt(id));
        mv.addObject("prlist",products);
        mv.addObject("guideslist",guides);
        mv.setViewName("ProductandGuideAll");
        return mv;
    }



}
